/*
https://leetcode-cn.com/problems/stock-price-fluctuation/solution/gu-piao-jie-ge-bo-dong-by-leetcode-solut-rwrb/
 */
import java.util.HashMap;
import java.util.TreeMap;

public class Solution2034 {

    int max=0;
    HashMap<Integer,Integer> map1=new HashMap<>();
    TreeMap<Integer,Integer> map2=new TreeMap<>();

    public Solution2034() {

    }

    public void update(int timestamp, int price) {
        max=Math.max(timestamp,max);
        if (map1.containsKey(timestamp)){
            int m=map1.get(timestamp);
            map2.put(m,map2.get(m)-1);
            if (map2.get(m)==0){
                map2.remove(m);
            }
        }
        map2.put(price,map2.getOrDefault(price,0)+1);
        map1.put(timestamp,price);
    }

    public int current() {
        return map1.get(max);
    }

    public int maximum() {
        return map2.lastKey();
    }

    public int minimum() {
        return map2.firstKey();
    }

    public static void main(String[] args) {
        Solution2034 solution2034=new Solution2034();
        solution2034.update(1,10);
        solution2034.update(2,5);
        System.out.println(solution2034.current());
        System.out.println(solution2034.maximum());
        solution2034.update(1,3);
        System.out.println(solution2034.maximum());
        solution2034.update(4,2);
        System.out.println(solution2034.minimum());
    }
}
